It took a bit of digging to come up with the steps required to get basic unit testing working in XCode. These steps borrow heavily from the following sites (in no order, because I don't remember what all it took to get this working):
Much of my confusion comes from the difference between using "raw" Objective-C (relatively easy) versus XCode and all that implies. In any case, that's the environment I'll be using for courses, so that's what I need to be able to get working.
What These Steps Describe
Technology:
XCode 3.2.1
The OCunit framework built-in to XCode 3.2.1
Cocoa Framework
Snow Leopard, though I suspect this will all work under any variant of 10.3 or later
These steps describe what you'll need to do to set up a framework, not an application. If you want to set up an application, review either of the bottom two links in the Why section.
In the top-most right pane, selectUnit Test Bundle and eitherdouble-click or clicknext
UnderTarget Name enter some descriptive name. I'll useUnitTests.
ClickFinish
This should bring up the info window on the target:
Under the Direct Dependencies pane, click the+ and add a dependency toRpnCalculator (double-click onRpnCalculator)
Close the info window
Adding a Test Fixture
Now you'll add a simple fixture with a failing test, then get the test to pass.
Create a new File,File::New orCommand-N
SelectCocoa class underMac OS X
In the upper-most right pane, selectObjective-C test case class
ClickNext (or double-click)
Under theFile Name enter some sensible test name (making sure to leave the ".m" at the end of the name alone). I'll be usingANewlyCreatedRpnCalculatorShould
Under theTargets make sure to selectUnitTests and to unselectRpnCalculator
Why?
It took a bit of digging to come up with the steps required to get basic unit testing working in XCode. These steps borrow heavily from the following sites (in no order, because I don't remember what all it took to get this working):Much of my confusion comes from the difference between using "raw" Objective-C (relatively easy) versus XCode and all that implies. In any case, that's the environment I'll be using for courses, so that's what I need to be able to get working.
What These Steps Describe
Technology:These steps describe what you'll need to do to set up a framework, not an application. If you want to set up an application, review either of the bottom two links in the Why section.
Video Version
TDD & Objective-C in XCode: Getting Started from Brett L. Schuchert on Vimeo.
Setting up Project
At this point, you should have a window that resembles the following:
Configuring for Unit Tests
Adding a Test Fixture
Now you'll add a simple fixture with a failing test, then get the test to pass.Create a Failing Test
Build to run the test
Let's assume the for building you'll want to run the tests every time.Get the test to pass
- Update the assertion line in ANewlyCreatedCalculatorShould from:
To:Really Getting it all Tied Together
Now it's time to get the RpnCalculator framework linked into the test target.This example imports a type that does not yet exist. So add it:
If you build now, you will get a linking error. Try it:
To Fix this:
Congratulations
You have a project configured to run unit tests when built.- You can add "production" code to RpnCalculator and unit tests code to UnitTests.
- By default, a build will build the RpnCalculator framework, then UnitTests and then execute the unit tests.
At this point, you can continue practicing Test Driven Development.